package com.jzo2o.customer.service.impl;

import cn.hutool.core.util.ObjectUtil;


import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.jzo2o.api.customer.dto.response.AddressBookResDTO;
import com.jzo2o.common.expcetions.CommonException;
import com.jzo2o.common.expcetions.ForbiddenOperationException;
import com.jzo2o.common.model.PageResult;
import com.jzo2o.common.utils.BeanUtils;
import com.jzo2o.common.utils.CollUtils;
import com.jzo2o.customer.mapper.AddressBookMapper;
import com.jzo2o.customer.model.domain.AddressBook;
import com.jzo2o.customer.model.dto.request.AddressBookPageQueryReqDTO;
import com.jzo2o.customer.model.dto.request.AddressBookUpsertReqDTO;
import com.jzo2o.customer.service.IAddressBookService;
import com.jzo2o.mvc.utils.UserContext;
import com.jzo2o.mysql.utils.PageHelperUtils;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;
import java.util.List;
import java.util.stream.Collectors;

/**
 * <p>
 * 地址薄 服务实现类
 * </p>
 *
 * @author itcast
 * @since 2023-07-06
 */
@Service
public class AddressBookServiceImpl extends ServiceImpl<AddressBookMapper, AddressBook> implements IAddressBookService {

    @Override
    public List<AddressBookResDTO> getByUserIdAndCity(Long userId, String city) {

        List<AddressBook> addressBooks = lambdaQuery()
                .eq(AddressBook::getUserId, userId)
                .eq(AddressBook::getCity, city)
                .list();
        if(CollUtils.isEmpty(addressBooks)) {
            return new ArrayList<>();
        }
        return BeanUtils.copyToList(addressBooks, AddressBookResDTO.class);
    }

    @Override
    public void saveAddressBook(AddressBookUpsertReqDTO addressBookUpsertReqDTO) {
        if (ObjectUtil.isNull(addressBookUpsertReqDTO)) {
            throw new CommonException("地址薄为空");
        }

        AddressBook addressBook = BeanUtils.copyProperties(addressBookUpsertReqDTO, AddressBook.class);
        addressBook.setUserId(UserContext.currentUserId());
        if (addressBook.getIsDefault()==1){
            lambdaUpdate().set(AddressBook::getIsDefault,0)
                    .eq(AddressBook::getIsDefault,1)
                    .eq(AddressBook::getUserId,UserContext.currentUserId())
                    .update();
        }
        save(addressBook);


    }

    @Override
    public PageResult<AddressBook> getByPage(AddressBookPageQueryReqDTO addressBookPageQueryReqDTO) {
        return PageHelperUtils.selectPage(addressBookPageQueryReqDTO,()->lambdaQuery().eq(AddressBook::getUserId,UserContext.currentUserId()).list());


    }

    @Override
    public void updateAddressById(Long id, AddressBookUpsertReqDTO addressBookUpsertReqDTO) {
        boolean update = lambdaUpdate().eq(AddressBook::getId, id)
                .set(AddressBook::getIsDefault, addressBookUpsertReqDTO.getIsDefault())
                .set(AddressBook::getCity, addressBookUpsertReqDTO.getCity())
                .set(AddressBook::getAddress, addressBookUpsertReqDTO.getAddress())
                .set(AddressBook::getPhone, addressBookUpsertReqDTO.getPhone())
                .set(AddressBook::getCounty, addressBookUpsertReqDTO.getCounty())
                .set(AddressBook::getProvince, addressBookUpsertReqDTO.getProvince())
                .set(AddressBook::getName, addressBookUpsertReqDTO.getName())
                .update();
        if(!update){
            throw new CommonException("修改失败！");
        }
    }

    @Override
    public void batchDelete(List<String> ids) {
        if (CollUtils.isEmpty(ids)) {
            throw new CommonException("删除失败！");
        }

        List<Long> collect = ids.stream().map(Long::valueOf).collect(Collectors.toList());
        boolean b = this.removeByIds(collect);
        if(!b){
            throw new CommonException("删除失败！");
        }


    }

    @Override
    @Transactional
    public void setDefault(Integer flag, Long addressBookId) {
        Long userId = UserContext.currentUserId();
        AddressBook one = lambdaQuery().eq(AddressBook::getUserId, userId)
                .eq(AddressBook::getId, addressBookId)
                .one();
        if (ObjectUtil.isNull(one)) {
            throw new ForbiddenOperationException("当前用户不能操作！");
        }

        boolean update1 = lambdaUpdate().set(AddressBook::getIsDefault, 0).eq(AddressBook::getUserId, userId).update();
        boolean update2 = lambdaUpdate().set(AddressBook::getIsDefault, 1).eq(AddressBook::getId, addressBookId).update();
        if (!update1|| !update2) {
            throw new CommonException("设置失败");
        }
    }

    @Override
    public AddressBook getDefault() {
        return lambdaQuery().eq(AddressBook::getIsDefault, 1).eq(AddressBook::getUserId,UserContext.currentUserId()).one();
    }


}
